package com.rongc.client.core.utils;

import android.os.Handler;

import java.util.HashMap;

/**
 * Created by ccqx on 2017/6/7.
 * 进度计时
 */

public class CountDownUtil {


    public static final String VERIFIY_TYPE = "VERIFIY_TYPE";  //
    public static final String VERIFIY_PASSWORD_TYPE = "VERIFIY_PASSWORD_TYPE";  //

    static final int DEF_DURATION = 60;

    HashMap<String, CountDownRunnable> runables; // 倒计时集合
    HashMap<String, Integer> durations; // 定时时间
    HashMap<String, Integer> countdowns; // 倒计时时间

    private static CountDownUtil instance;

    public static CountDownUtil getInstance() {
        if (instance == null) instance = new CountDownUtil();
        return instance;
    }

    public CountDownUtil() {
        runables = new HashMap<>();
        runables.put(VERIFIY_TYPE, new CountDownRunnable(null, VERIFIY_TYPE));

        durations = new HashMap<>();
        durations.put(VERIFIY_TYPE, DEF_DURATION);

        countdowns = new HashMap<>();
        countdowns.put(VERIFIY_TYPE,0);
    }

    public void initRunnable(Handler handler, String type){
        CountDownRunnable runRegisterCode = runables.get(type);
        if (runRegisterCode == null) {
//            runRegisterCode = new CountDownRunnable(handler, type);
            runables.put(type, new CountDownRunnable(handler, type));
            durations.put(type, DEF_DURATION);
            countdowns.put(type,0);
        } else {
            runRegisterCode.setHandler(handler);
        }

    }

    public void initRunnable(String type) {
        getRunnable(type).init();
    }

    public void setDuration(String type, int duration) {
        durations.put(type, duration);
    }

    public CountDownRunnable getRunnable(String type){
        CountDownRunnable runRegisterCode = runables.get(type);
        return runRegisterCode;
    }


    public int getCountDown(String type) {
        return getRunnable(type).getCountDown();
    }

    public int getDuration(String type) {
        return getRunnable(type).getDuration();
    }

    public void setProgress(String type, int progress) {
        getRunnable(type).setProgress(progress);
    }

    public void Pause(String type) {
        getRunnable(type).pause();
    }

    public void Stop(String type) {
        getRunnable(type).stop();
    }

    public void Play(String type) {
        getRunnable(type).play();
    }

    public class CountDownRunnable implements Runnable {
        Handler handler;
        boolean isRun;
        boolean isPause;  // 是否暂停
        String type;  //

        public CountDownRunnable(Handler handler, String type) {
            this.handler = handler;
            this.type = type;
        }

        void setHandler(Handler handler) {
            this.handler = handler;
        }

        @Override
        public void run() {
            if (isRun) return;
            do {
                isRun = true;
                if (!isPause) {
                    countdown();
                    if (handler != null)
                        handler.sendEmptyMessage(0);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (getCountDown() < getDuration() && isRun);
            if (handler != null)
                handler.sendEmptyMessage(1);
            init();
            isRun = false;
        }

        private void countdown() {
            int temp = countdowns.get(type);
            countdowns.put(type,++temp);
        }

        void init() {
            isRun = false;
            isPause = false;

            countdowns.put(type,0);
        }

        void setProgress(int progress){
            countdowns.put(type,progress);
        }


        int getCountDown() {
            return countdowns.get(type);
        }

        int getDuration() {
            return durations.get(type);
        }


        public void play() {
            isPause = false;
        }

        public void pause() {
            isPause = true;
        }

        public void stop(){
            isRun = false;
        }
    }
}
